<?php

define('SS_CLI_MODE',(!$_SERVER['DOCUMENT_ROOT']));
//print(SS_CLI_MODE?'cli':'web');
require_once 'spyc/spyc.php';
require_once 'Bucket/bucket.inc.php';
require_once('ss/core/ss_core.php');
require_once('ss/core/ss_core_file.php');
require_once('doctrine12/Doctrine.php');
//require_once('doctrine/Doctrine.compiled.php');
spl_autoload_register(array('Doctrine', 'autoload'));
spl_autoload_register('ss_core_file::autoload');
require_once('site/core/site_core.php');
$ss=new site_core_config($config.'.config',SITE_ROOT_PATH.DIRECTORY_SEPARATOR,SS_CLI_MODE);
$ss->merge(new ss_core_serverConfig('slipstream.config',SS_ROOT_PATH.DIRECTORY_SEPARATOR,SS_CLI_MODE));

if($ss['error']['firephp']['active']){
	require_once('FirePHPCore/FirePHP.class.php');
	require_once('FirePHPCore/fb.php');
	require_once('ss/error/ss_error_firephp.php');
	$firephp = FirePHP::getInstance(true);
	
	$firephp->registerErrorHandler($throwErrorExceptions=true);
	$firephp->registerExceptionHandler();
	$firephp->registerAssertionHandler( $convertAssertionErrorsToExceptions=true, $throwAssertionExceptions=false);
	
	$options = array('maxObjectDepth' => 1,
	                 'maxArrayDepth' => 4,
	                 'useNativeJsonEncode' => true,
	                 'includeLineNumbers' => true);
	$firephp->setOptions($options);
	FB::setEnabled(true);
	error_reporting(E_ALL | E_STRICT);
} else {
	require_once('ss/error/ss_error_dummy.php');
	file_put_contents(SITE_ROOT_PATH.DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR.ss_error::$log,"Debug log:\n\n");
}
ss_error::group('Bootstrap');
//ss_error::log($ss['server'],'Settings');
ss_error::group('Dependency Injector setup');
if(class_exists('site_bucket_factory')){
	$factory=new site_bucket_factory($ss);
} else {
	$factory=new ss_bucket_factory($ss);
};
ss_error::log($ss,'Settings');
$injector=new ss_bucket_injector($factory);
$injector->registerImplementation('ss_core_configInterface','site_core_config');
$injector->set($ss);
//ss_error::log($injector,'Injector');
ss_error::groupEnd();

if($ss['doctrine']['active']){
	//require_once('doctrine/Doctrine/Export.php');
	ss_error::group('Doctrine setup');
	define('SS_DOCTRINE_PATH',SS_ROOT_PATH.DIRECTORY_SEPARATOR.'doctrine');
	define('SS_DOCTRINE_DATA_FIXTURES_PATH',SS_DOCTRINE_PATH.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'fixtures');
	define('SS_DOCTRINE_MODELS_PATH',SS_DOCTRINE_PATH.DIRECTORY_SEPARATOR.'models');
	define('SS_DOCTRINE_MIGRATIONS_PATH',SS_DOCTRINE_PATH.DIRECTORY_SEPARATOR.'migrations');
	define('SS_DOCTRINE_SQL_PATH',SS_DOCTRINE_PATH.DIRECTORY_SEPARATOR.'sql');
	define('SS_DOCTRINE_SCHEMA_PATH',SS_DOCTRINE_PATH.DIRECTORY_SEPARATOR.'schema');
	$injector->set($manager = Doctrine_Manager::getInstance());
	//$manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
	$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
	//$manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
	$manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);
	$manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);
	$manager->setAttribute('portability', 'none');
	$_cstring=$ss['dao']['default']['type'].'://'.
		$ss['dao']['default']['user'].':'.
		$ss['dao']['default']['pass'].'@'.
		$ss['dao']['default']['host'].'/'.
		$ss['dao']['default']['database']/*.
		'?charset=utf8'*/;
	if(isset($ss['dao']['default']['unix_socket'])) $_cstring.=";unix_socket={$ss['dao']['default']['unix_socket']}";
	ss_error::log($_cstring);
	$injector->set($conn = Doctrine_Manager::connection($_cstring, 'default'));
	$conn->setCharset('utf8'); 
	$conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
	//$conn->setCharset(’UTF8′);
	ss_error::groupEnd();

}


spl_autoload_register(array('Doctrine', 'autoload'));
require_once 'konstrukt/konstrukt.inc.php';
require_once 'konstrukt/response.inc.php';
require_once 'Smarty/Smarty.class.php';
?>